package com.finddeer.bookkeeping.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.finddeer.bookkeeping.Bill;
import com.finddeer.bookkeeping.BillLab;
import com.finddeer.bookkeeping.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : leaves
 * gitee     : https://gitee.com/LEAVES2018
 * time      : 2018/12/23 8:22 PM
 * function  :
 */
//  TODO 为趋势页面添加顶部导航，并解决TabLayout与LineChart等图表类的位置冲突
public class TendencyFragment extends Fragment {

    @BindView(R.id.lineChart)
    LineChart mLineChart;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void updateLineChart() {
        List<Entry> mValues = new ArrayList<>();

        BillLab billLab = BillLab.get(getActivity());
        List<Bill> bills = billLab.getBills();

        for (int i = 0; i < bills.size(); i++) {
            mValues.add(new Entry(i, (float) bills.get(i).getAmount()));
        }

        List<ILineDataSet> lineDataSets = new ArrayList<>();
        LineDataSet lineDataSet = new LineDataSet(mValues, "");
        lineDataSets.add(lineDataSet);

        /*Description description = new Description();
        description.setText("一月");*/
        //lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSet.setLineWidth(3f);//线宽
        lineDataSet.setColor(Color.rgb(58,143,183));//现实颜色
        lineDataSet.setHighLightColor(Color.WHITE);//高度线的颜色
        mLineChart.setDrawBorders(false);//是否添加边框
        mLineChart.setNoDataText("我需要数据");//没数据显示
        mLineChart.setDrawGridBackground(false);//是否显示表格颜色
        mLineChart.setBackgroundColor(Color.rgb(200,200,200));//背景颜色
        Legend legend = mLineChart.getLegend();//设置比例图片标示，就是那一组Y的value
        legend.setForm(Legend.LegendForm.CIRCLE);//样式
        legend.setFormSize(6f);//字体
        legend.setTextColor(Color.WHITE);//设置颜色
        //mLineChart.animateX(2000);//
        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        /*YAxis leftAxis = mLineChart.getAxisLeft();
        leftAxis.setDrawLabels(false);*/

        LineData data = new LineData(lineDataSets);
        mLineChart.setData(data);
        mLineChart.invalidate();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tendency, container, false);
        ButterKnife.bind(this, view);

        updateLineChart();

        return view;
    }
}